﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using BL;


namespace StockVentas
{
    public partial class frmPrincipal : Form
    {
        public frmProgress progreso;
        string origen, accion;

        public frmPrincipal()
        {
            InitializeComponent();
            foreach (Control control in this.Controls)
            {
                MdiClient client = control as MdiClient;
                if (!(client == null))
                {
                    client.BackColor = this.BackColor;
                    break;
                }
            }
        }

        private void frmPrincipal_Load(object sender, EventArgs e)
        {
            System.Drawing.Icon ico = Properties.Resources.icono_app;
            this.Icon = ico;
        }

        private void articulosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            origen = "frmArticulos";
            accion = "cargar";
            frmArticulos newMDIChild = new frmArticulos();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void artículosItemsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmArticulosItems newMDIChild = new frmArticulosItems();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void clientesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmClientes newMDIChild = new frmClientes();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void coloresToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmColores newMDIChild = new frmColores();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void formasDePagoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmFormasPago newMDIChild = new frmFormasPago();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void génerosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmGeneros newMDIChild = new frmGeneros();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void proveedoresToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmProveedores newMDIChild = new frmProveedores();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void salirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void movimientosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmStockMov newMDIChild = new frmStockMov();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void StockEntradas_Click(object sender, EventArgs e)
        {
            frmStockEntradas newMDIChild = new frmStockEntradas();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void StockCompensaciones_Click(object sender, EventArgs e)
        {
            frmStockComp newMDIChild = new frmStockComp();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void ventasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmVentas newMDIChild = new frmVentas();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void movimientosDeTesoreríaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //frmTesoreriaMov
            //frmTesoreriaBinding
            frmTesoreriaMov newMDIChild = new frmTesoreriaMov();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void fondoDeCajaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmFondoCaja newMDIChild = new frmFondoCaja();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void stockDeArtículosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmStockInter newMDIChild = new frmStockInter();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void movimientosToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmStockMovInter newMDIChild = new frmStockMovInter();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void compensacionesToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmStockCompInter newMDIChild = new frmStockCompInter();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void arqueoDeCajaToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmArqueoInter newMDIChild = new frmArqueoInter();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void ventasEnPesos_Click(object sender, EventArgs e)
        {
            frmVentasPesosInter newMDIChild = new frmVentasPesosInter();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void ventasDetalle_Click(object sender, EventArgs e)
        {
           frmVentasDetalleInter newMDIChild = new frmVentasDetalleInter();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void ventasHistoricas_Click(object sender, EventArgs e)
        {
            frmVentasHistoricasInter newMDIChild = new frmVentasHistoricasInter();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void fondosDeCajaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            origen = "frmFondoCajaCons";
            accion = "cargar";
            frmProgress newMDIChild = new frmProgress(origen, accion);
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void pedidos_Click(object sender, EventArgs e)
        {
            frmPedido newMDIChild = new frmPedido();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void actualizarServidorToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
        /*   frmActualizarStock newMDIChild = new frmActualizarStock();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();*/
        }

        private void backup_Click(object sender, EventArgs e)
        {
            SaveFileDialog fichero = new SaveFileDialog();
            fichero.Filter = "SQL (*.sql)|*.sql";
            if (fichero.ShowDialog() == DialogResult.OK)
            {
                System.IO.StreamWriter sw = System.IO.File.CreateText("c:\\Windows\\Temp\\backup.bat"); // creo el archivo .bat
                sw.Close();
                StringBuilder sb = new StringBuilder();
                string path = Application.StartupPath;
                string unidad = path.Substring(0, 2);
                sb.AppendLine(unidad);
                sb.AppendLine(@"cd " + path + @"\Backup");
                sb.AppendLine(@"mysqldump --skip-comments -u ncsoftwa_re -p8953#AFjn -h dns26.cyberneticos.com --opt ncsoftwa_re > " + fichero.FileName);
                using (StreamWriter outfile = new StreamWriter("c:\\Windows\\Temp\\backup.bat", true)) // escribo el archivo .bat
                {
                    outfile.Write(sb.ToString());
                }
                frmProgress frm = new frmProgress("backup", "cargar");
                frm.Show();
            }
        }

        private void processBackupExited(object sender, System.EventArgs e)
        {
            MessageBox.Show("Se guardó una copia de seguridad de la base de datos en c:\\Windows\\Temp\\backup.sql");
            if (File.Exists("c:\\Windows\\Temp\\backup.bat"))
            {
                File.Delete("c:\\Windows\\Temp\\backup.bat");
            }            
        }

        private void frmPrincipal_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void pruebasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmPruebas newMDIChild = new frmPruebas();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

    }
}
